var helper = "helper";

var login = function() {

    var username = $("#username").val();
    var password = $("#password").val();
    var token    = $("#token").val();
    var checked  = ($("#check-field").attr("checked") == "checked" ? true : false);

    if(username.length > 0 && password.length > 0) {
        $.ajax({
            url: base_url + 'admin/login',
            type: "post",
            data: {username: username, password: password, token: token, checked: checked},
            success: function(data) {
                console.log("Returned value " + data);
                if(data == "Could not log in") {
                    alert("Fel användarnamn eller lösenord");
                }
                else {
                    window.location.href = base_url+"admin/index";
                    //console.log(data);
                }
            },
            error: function(error) {
                alert(error);
            }
        });
    } // end if

}

var logout = function() {
    $.ajax({
        url: base_url + 'admin/logout',
        type: "post",
        data: {option: "logout"},
        success: function(data) {
            console.log("Returned value" + data);
            window.location.href = base_url + 'admin/loginView';
        },
        error: function(error) {

        }
    })
}

var spinnerOverlayShow = function() {
    // Create overlay and append to body:
    $('<div id="overlay"/>').css({
        position: 'fixed',
        top: 0,
        left: 0,
        width: '100%',
        height: $(window).height() + 'px',
        background: 'url('+base_path+'img/ajax-loader.gif) no-repeat center'
    }).hide().appendTo('body');
}

var deleteInfo = function() {
    //START NOTIFICATION CLOSE
    $('.notification .close').click(function(){
        $(this).parent().parent().animate({top: '25px', opacity: 0}, 500, function() {
            $(this).slideUp();
        });
    });
    //END NOTIFICATION CLOSE
}

var addMenu = function() {
    $.get(base_url + 'admin/addmenu', function(data) {
        $("#getAjaxPageContent").html(data);
        deleteInfo();
        $("#accordion").accordion({ header: "h3" });
    });
}

var addPage = function() {
    $.get(base_url + 'admin/addPage', function(data) {
        $("#getAjaxPageContent").html(data);
        deleteInfo();
    });
}

var managePage = function() {
    $.get(base_url + 'admin/managePages', function(data) {
       $("#getAjaxPageContent").html(data);
        deleteInfo();
    });
}

var gallery = function() {
    $.get('gallery.php', function(data) {
        $("#getAjaxPageContent").html(data);
        deleteInfo();
    });
}

var addNews = function() {
    $.get('addNews.php', function(data) {
        $("#getAjaxPageContent").html(data);
        deleteInfo();
    });
}

var statistics = function() {
    $.get('statistics.php', function(data) {
        $("#getAjaxPageContent").html(data);
        deleteInfo();
    });
}

var serverStatus = function() {
    $.get('serverStatus.php', function(data) {
        $("#getAjaxPageContent").html(data);
        deleteInfo();
    });
}

var appendMenu = function() {
    
    var name = $("#menu_name").val();
    var parent = $("#parent_menu").val();
    
    spinnerOverlayShow();

    if(name.length > 0) {
        $('#overlay').show();
        $.ajax({
            url: base_url+"admin/appendMenu",
            type: "post",
            data: { label: name, parent: parent},
            success: function(data) {
                console.log("Returned value: "+data);
                $('#overlay').hide();
                $("#menu_name").val("");
            },
            error: function(error) {
                alert(error);
            }
        });
    }else {
        $(".showError").html("<b>Du måste fylla i ett namn</b>");
        setTimeout(function() {
            $(".showError").html("");
        }, 2500);
    }
}

var appendPage = function() {
    
    var title = $("#page_title").val();
    var parent = $("#parent_page").val();
    
    spinnerOverlayShow();

    if(title.length > 0) {
        $('#overlay').show();
        var request = $.ajax({
            url: base_url + "admin/appendPage",
            type: "post",
            data: {
                title: title,
                parent: parent
            },
            success: function(data) {
                console.log("Returned value" + data);
                $('#overlay').hide();
            },
            error: function(error) {
                alert(error);
            }
        });
    }
    else {
        
    }
}